package com.example.qizhong.controller;

import com.example.qizhong.model.Student;
import com.example.qizhong.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {
    @Autowired
    StudentService studentService;
    //查询所有用户信息
    @GetMapping("/student")
    public List<Student> getAllStudent(){
        return studentService.list();

    }
    //根据id查询指定用户信息
    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable("id") Integer id){
        Student student = studentService.getById(id);
        return student;
    }
    //插入用户信息
    @PostMapping("/student")
    public String addStudent(@RequestBody Student student){
        if (studentService.save(student)){
            return "插入用户成功！";
        }
        else return "插入用户失败";
    }
    //更新用户信息
    @PutMapping("/student")
    public String updateStudent(@RequestBody Student student){
        if (studentService.updateById(student)){
            return "更新用户成功！";
        }
        else return "更新用户失败";
    }
    //根据id删除指定用户
    @DeleteMapping("/student")
    public String deleteStudent(Integer id){
        if (studentService.removeById(id)) {
            return "删除成功！";
        }
        else return "删除失败！";
    }

}
